package com.yangjian.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yangjian.bean.Orders;
import com.yangjian.comon.R;
import com.yangjian.dto.OrderDto;
import com.yangjian.page.PageParameter;
import com.yangjian.service.OrdersService;
import com.yangjian.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/order")
@RestController
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders) {

        int row = ordersService.add(orders);
        System.out.println("11111111111111111111111111111111111111");
        System.out.println("row = " + row);
        System.out.println("22222222222222222222222222222222222222222222222");
        if (row > 0) {
            return R.success("订单提交成功!");

        } else {
            return R.error("订单提交失败！");
        }

    }

    /**
     * 手机端查询订单
     * @param pageParameter
     * @return
     */
    @GetMapping("/userPage")
    public R findPage(PageParameter pageParameter) {

        try {
            IPage<OrderDto> page = ordersService.findPage(pageParameter, BaseContext.getCurrentId());
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("订单分页查询失败");
        }
    }


    /**
     * 电脑端分页查询订单
     * @param pageParameter
     * @return
     */
    @GetMapping("/page")
    public R findPage1(PageParameter pageParameter) {

        System.out.println("pageParameter = " + pageParameter);

        try {
            IPage<OrderDto> page = ordersService.findPage1(pageParameter);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("订单分页查询失败");
        }


    }


    @PutMapping
    public R update(@RequestBody Orders orders){

        int row = ordersService.update(orders);

        if (row > 0) {
            return R.success("修改订单成功!");

        } else {
            return R.error("修改订单失败！");
        }

    }
}
